package com.sky.controller.user;

import com.sky.dto.OrdersPaymentDTO;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrdersService;
import com.sky.vo.HistoryOrdersVO;
import com.sky.vo.OrderPaymentVO;
import com.sky.vo.OrderSubmitVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@Api("用户订单相关接口")
@Slf4j
@RestController("userOrdersController")
@RequestMapping("/user/order")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;

    /**
     * 用户下单
     * @param ordersSubmitDTO
     * @return
     */
    @ApiOperation("用户下单")
    @PostMapping("/submit")
    public Result<OrderSubmitVO> submit(@RequestBody OrdersSubmitDTO ordersSubmitDTO) {
        log.info("用户下单:{}", ordersSubmitDTO);
        OrderSubmitVO orderVO = ordersService.submit(ordersSubmitDTO);
        return Result.success(orderVO);
    }

    /**
     * 订单支付
     *
     * @param ordersPaymentDTO
     * @return
     */
    @PutMapping("/payment")
    @ApiOperation("订单支付")
    public Result<OrderPaymentVO> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) throws Exception {
        log.info("订单支付：{}", ordersPaymentDTO);
        OrderPaymentVO orderPaymentVO = ordersService.payment(ordersPaymentDTO);
        log.info("生成预支付交易单：{}", orderPaymentVO);
        return Result.success(orderPaymentVO);
    }

    /**
     * 获取历史订单
     * @param page
     * @param pageSize
     * @return
     */
    @ApiOperation("获取历史订单")
    @GetMapping("/historyOrders")
    public Result<PageResult> page(@RequestParam Integer page , @RequestParam Integer pageSize) {
        log.info("获取历史订单，页面:{}, 每页记录数：{}", page, pageSize);
        PageResult pageResult = ordersService.getHistoryOrders(page, pageSize);
        return Result.success(pageResult);
    }

    /**
     * 获取订单详情
     * @param id
     * @return
     */
    @ApiOperation("获取订单详情")
    @GetMapping("/orderDetail/{id}")
    public Result<HistoryOrdersVO> OrdersDetail(@PathVariable Long id) {
        log.info("获取订单详情:{}", id);
        HistoryOrdersVO historyOrdersVO = ordersService.ordersDetail(id);
        return Result.success(historyOrdersVO);
    }

    /**
     * 取消订单
     * @param id
     * @return
     */
    @ApiOperation("取消订单")
    @PutMapping("/cancel/{id}")
    public Result CancelOrders(@PathVariable Long id) {
        log.info("获取订单详情:{}", id);
        ordersService.userCancelOrders(id);
        return Result.success();
    }

    /**
     * 再来一单
     * @param id
     * @return
     */
    @ApiOperation("再来一单")
    @PostMapping("/repetition/{id}")
    public Result RepetitionOrders(@PathVariable Long id) {
        log.info("获取订单详情:{}", id);
        ordersService.RepetitionOrders(id);
        return Result.success();
    }

}
